﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////  LIBRERIA: SIMA.CORE                                                                                             ////
////  DESARROLLADO POR JUAN CARLOS VEGA NEIRA                                                                         ////
////  UNIVERSIDAD RICARDO PALMA                                                                                       ////
////  CURSO DE TALLER DE GERENTCIA DE PROYECTOS                                                                       ////
////  SEMESTRE 2010 - II                                                                                              ////
////  SEPTIEMBRE, 2010                                                                                                ////
////  TODOS LOS DERECHOS RESERVADOS                                                                                   ////
////  VERSIÓN v1.0                                                                                                    ////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Text.RegularExpressions;
using System.Reflection;

using SIMA.Core.Data.Validators;
using SIMA.Core.Exceptions;


namespace SIMA.Core.Utils
{
    public static class ValidatorUtils<T>
    {
        public static void ValidationRules(PropertyInfo property, T item)
        {
            #region Required Validation

            if (Attribute.GetCustomAttribute(property, typeof(Required)) != null)
            {
                Attribute attr = Attribute.GetCustomAttribute(property, typeof(Required));
                Required required = (Required)attr;
                if (property.GetValue(item, null) == null)
                {
                    if (required.ErrorMessage != string.Empty)
                    {
                        throw new ValidationException(required.ErrorMessage);
                    }
                    else
                    {
                        throw new ValidationException();
                    }
                }
            }

            #endregion

            #region MaxLength Validation

            if (Attribute.GetCustomAttribute(property, typeof(MaxLengthString)) != null)
            {
                Attribute attr = Attribute.GetCustomAttribute(property, typeof(MaxLengthString));
                MaxLengthString maxLength = (MaxLengthString)attr;
                if (property.GetValue(item, null).ToString().Length > maxLength.MaxLength)
                {
                    if (maxLength.ErrorMessage != string.Empty)
                    {
                        throw new ValidationException(maxLength.ErrorMessage);
                    }
                    else
                    {
                        throw new ValidationException();
                    }
                }
            }

            #endregion

            #region Maximum Validation

            if (Attribute.GetCustomAttribute(property, typeof(Maximum)) != null)
            {
                Attribute attr = Attribute.GetCustomAttribute(property, typeof(Maximum));
                Maximum maxVal = (Maximum)attr;
                if (Convert.ToDouble(property.GetValue(item, null).ToString()) > maxVal.MaxValue)
                {
                    if (maxVal.ErrorMessage != string.Empty)
                    {
                        throw new ValidationException(maxVal.ErrorMessage);
                    }
                    else
                    {
                        throw new ValidationException();
                    }
                }
            }

            #endregion

            #region Minimum Validation

            if (Attribute.GetCustomAttribute(property, typeof(Minimum)) != null)
            {
                Attribute attr = Attribute.GetCustomAttribute(property, typeof(Minimum));
                Minimum minVal = (Minimum)attr;
                if (Convert.ToDouble(property.GetValue(item, null).ToString()) < minVal.MinVal)
                {
                    if (minVal.ErrorMessage != string.Empty)
                    {
                        throw new ValidationException(minVal.ErrorMessage);
                    }
                    else
                    {
                        throw new ValidationException();
                    }
                }
            }

            #endregion

            #region Range Validation

            if (Attribute.GetCustomAttribute(property, typeof(Range)) != null)
            {
                Attribute attr = Attribute.GetCustomAttribute(property, typeof(Range));
                Range range = (Range)attr;
                if (range.MinValue > range.MaxValue || range.MinValue == range.MaxValue)
                {
                    throw new CoreFaultException();
                }
                else
                {
                    if (Convert.ToDouble(property.GetValue(item, null).ToString()) < range.MinValue || Convert.ToDouble(property.GetValue(item, null).ToString()) > range.MaxValue)
                    {
                        if (range.ErrorMessage != string.Empty)
                        {
                            throw new ValidationException(range.ErrorMessage);
                        }
                        else
                        {
                            throw new ValidationException();
                        }
                    }
                }

            }

            #endregion

            #region Regular Expression Validation

            if (Attribute.GetCustomAttribute(property, typeof(RegularExpression)) != null)
            {
                Attribute attr = Attribute.GetCustomAttribute(property, typeof(RegularExpression));
                RegularExpression regEx = (RegularExpression)attr;

                Regex regVal = new Regex(regEx.RegExpression);
                if (!regEx.Match(property.GetValue(item, null)))
                {
                    if (regEx.ErrorMessage != string.Empty)
                    {
                        throw new ValidationException(regEx.ErrorMessage);
                    }
                    else
                    {
                        throw new ValidationException();
                    }
                }


            }

            #endregion
        }
    }
}
